//
//  RecommedViewController.m
//  carowner
//
//  Created by apple on 2019/5/21.
//  Copyright © 2019 ggj. All rights reserved.
//

#import "RecommedViewController.h"
#import "ServiceShopCell.h"
#import "YLButton.h"
#import "RecommendShopParams.h"
#import "ShopListEntity.h"
#import "ShopDetailsViewController.h"
#import "CleanOrderController.h"

@interface RecommedViewController ()<OnItemClick>

@property(nonatomic,assign)NSInteger * page;
@property(nonatomic,strong)UITextField * searchView;
@property(nonatomic,strong)UIButton * btnSearch;
@property(nonatomic,strong)YLButton * btnSort;
@property(nonatomic,strong)RecommendShopParams * shopParams;
@property(nonatomic,strong)UIActivityIndicatorView * uiView;

@end

@implementation RecommedViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}


-(void)initData{
    
    _page = 0;
    _uiView = [NSObject getIndicatorView];
//    2019-05-22 14:36:21.611 8707-18864/com.sk.carowner V/RetrofitLog: retrofitBack = {"customerId":"ee96d0de14914b9a9a02229e1fd99a8e","disanceType":"-1","latitude":"32.393262","longitude":"119.539201","page":"0","serType":"","shopname":"","sortType":"-1","type":""}

    
    _shopParams = [[RecommendShopParams alloc] init];
    _shopParams.longitude = @"119.43";
    _shopParams.latitude = @"32.38";
    _shopParams.page =[NSString stringWithFormat:@"%d",(int)_page];
    _shopParams.disanceType = @"-1";
    _shopParams.serType = @"";
    _shopParams.sortType = @"-1";
    _shopParams.shopname = _searchView.text==nil?@"":_searchView.text;
    _shopParams.customerId = TEST_CUSTOMER_ID;
    _shopParams.type = @"";
    
    
    _searchView = [[UITextField alloc] init];
    _searchView.backgroundColor = edit_color;
    _searchView.layer.cornerRadius = 5;
    _searchView.placeholder = @"请输入门店名称";
    _searchView.font = textFont;
    _searchView.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
    //设置显示模式为永远显示(默认不显示 必须设置 否则没有效果)
    _searchView.leftViewMode = UITextFieldViewModeAlways;
    
    
    _btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnSearch setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    [_btnSearch addTarget:self action:@selector(doSearch) forControlEvents:UIControlEventTouchUpInside];
    
    _btnSort = [YLButton buttonWithType:UIButtonTypeCustom];
    [_btnSort setImage:[UIImage imageNamed:@"sort_icon"] forState:UIControlStateNormal];
    [_btnSort setTitle:@"筛选" forState:UIControlStateNormal];
    [_btnSort setTitleColor:tab_unselect_color forState:UIControlStateNormal];
    [_btnSort setFont:textFont];
    _btnSort.imageRect = CGRectMake(10, 10, 20, 20);
    _btnSort.titleRect = CGRectMake(35, 10, 30, 20);
    
    [self.view addSubview:_searchView];
    [self.view addSubview:_btnSearch];
    [self.view addSubview:_btnSort];
}
    

-(void)makeConstraint{
    [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.top.equalTo(self.view).offset(10);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width - 140, 40));
    }];
    
    [_btnSearch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_searchView.mas_right).offset(15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(_searchView);
    }];
    
    [_btnSort mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(_searchView);
        make.right.equalTo(self.view).offset(-30);
        make.centerY.equalTo(_searchView.mas_centerY);
    }];

    [_dataRyc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_searchView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
}

-(Class)getClass{
    return [ServiceShopCell class];
}

-(NSString *)getCellID{
    return @"cellID";
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ServiceShopCell * cell = (ServiceShopCell *)[self getCell];
    ShopListEntity * entity = [self getObjectAtIndex:indexPath];
    [MyUtils loadImageWithUrl:entity.imgsrc intoView:cell.logoImage];
    cell.shopName.text = entity.sertitle;
    cell.tvKm.text = [NSString stringWithFormat:@"%@%@",entity.distance,@"km"];
    cell.orderTime.text = [NSString stringWithFormat:@"%@-%@",entity.openTime,entity.closeTime];
    if((int)entity.isMy == 1){
        cell.addFav.hidden = YES;
    }else{
        cell.addFav.hidden = NO;
    }
    cell.pos = indexPath;
    cell.delegate = self;
    return cell;
}


-(void)onItemClick:(int)tag withPos:(NSIndexPath *)pos{
    if(tag == 1){//点击电话
        ShopListEntity * entity = [self getObjectAtIndex:pos];
        NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",entity.phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }else if(tag == 2){//点击预约
        CleanOrderController * orderVc = [[CleanOrderController alloc] init];
        ShopListEntity * entity = [self getObjectAtIndex:pos];
        orderVc.type = 1;
        orderVc.shopId = entity.orgId;
        orderVc.comID = entity.shopId;
        [self.navigationController pushViewController:orderVc animated:YES];
    }else if(tag == 3){//点击导航
        
    }else if(tag == 4){//点击加入收藏
        [_uiView startAnimating];
        ShopListEntity * entity = [self getObjectAtIndex:pos];
        NSDictionary * dct = [[NSDictionary alloc] initWithObjectsAndKeys:TEST_CUSTOMER_ID,@"customerId",entity.orgId,@"companyId", nil];
        [[ApiManager sharedManager] addShopToFav:dct andBlock:^(id data, NSError *error) {
            [_uiView stopAnimating];
            entity.isMy = 1;
            [_dataRyc reloadData];
        }];
    }
}


-(void)addMyFav:NS{
    
}
    

-(void)getListData{
    [[ApiManager sharedManager] getRecommendShopList:[_shopParams mj_JSONObject] andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ResultList];
        NSArray * dataArray = [ShopListEntity mj_objectArrayWithKeyValuesArray:resultData];
        [self finishLoadData:dataArray];
    }];
}

-(void)doRefreshData{
    _page = 0;
    _shopParams.page = [NSString stringWithFormat:@"%d",(int)_page];
    [[ApiManager sharedManager] getRecommendShopList:[_shopParams mj_JSONObject] andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ResultList];
        NSArray * dataArray = [ShopListEntity mj_objectArrayWithKeyValuesArray:resultData];
        [self finishRefreshData:dataArray];
    }];
}

-(void)doLoadData{
    _page++;
    _shopParams.page = [NSString stringWithFormat:@"%d",(int)_page];
    [[ApiManager sharedManager] getRecommendShopList:[_shopParams mj_JSONObject] andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ResultList];
        NSArray * dataArray = [ShopListEntity mj_objectArrayWithKeyValuesArray:resultData];
        [self finishLoadMoreData:dataArray];
    }];
}


-(void)doSearch{
    _page = 0;
    _shopParams.page = [NSString stringWithFormat:@"%d",(int)_page];
    _shopParams.shopname = _searchView.text;
    [_uiView startAnimating];
    [[ApiManager sharedManager] getRecommendShopList:[_shopParams mj_JSONObject] andBlock:^(id data, NSError *error) {
        [_uiView stopAnimating];
        id resultData = [data valueForKeyPath:ResultList];
        NSArray * dataArray = [ShopListEntity mj_objectArrayWithKeyValuesArray:resultData];
        [self finishLoadData:dataArray];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ShopDetailsViewController * detailsVc = [[ShopDetailsViewController alloc] init];
    ShopListEntity * entity = [self getObjectAtIndex:indexPath];
    detailsVc.shopId = entity.shopId;
    [self.navigationController pushViewController:detailsVc animated:YES];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
